package med.movyzer.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.TimeZone;

import org.jfree.data.time.Minute;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class GT3XData {
	private ArrayList<Integer[]> data=new ArrayList<Integer[]>();
	private String serialNumber;
	private String memoryAddress;
	private String batteryVoltage;
	private Date startTime;
	private Date downloadTime;
	private Date epochPeriod;
	
	private boolean isNumeric(String string){
		try{
			new Integer(string);
		}catch(Exception e){
			return false;
		}
		return true;
	}
	
	private boolean isMovement(Integer[] data){
		if(data[0]>0 || data[1]>0 || data[2]>0){
			return true;
		}
		return false;
	}
	
	private Integer getMovement(Integer[] data){
		return (data[0]+data[1]+data[2])/3;
	}
	
	public XYDataset getMovementDataSet(int size){
		TimeSeries series = new TimeSeries("Movement Graph",Second.class);		
		long currTime=startTime.getTime();//+delay.getTime();
		
		for(int i=0;(i+1)*size<data.size();i++){
			currTime=currTime+epochPeriod.getTime();
			
			int movSum=0;
			
			for(int n=1;n<size;n++){
				currTime=currTime+epochPeriod.getTime();
				Integer[] currData=data.get(i*size+n);
				movSum+=getMovement(currData);				
			}
			Date date=new Date(currTime);

			series.add(new Second(date.getSeconds(),date.getMinutes(),date.getHours(),
					date.getDay(),date.getMonth(),2010),movSum);
		}
		return new TimeSeriesCollection(series);
	}
	
	public void loadFile(String path) throws Exception{
		File file = new File(path);
		BufferedReader fReader  = new BufferedReader(new FileReader(file));
		
		String line = null;
		
		line = fReader.readLine();
		serialNumber = fReader.readLine().replace("Serial Number: ", "");
		
		SimpleDateFormat sdfToDate = new SimpleDateFormat( "dd.MM.yyyy HH:mm:ss" );
		String sStartTime = fReader.readLine().replace("Start Time ", "");
		String sStartDate = fReader.readLine().replace("Start Date ", "");
		startTime=sdfToDate.parse(sStartDate+" "+sStartTime);

		SimpleDateFormat sdf2ToDate = new SimpleDateFormat( "HH:mm:ss" );
		String sEpoch = fReader.readLine().replace("Epoch Period (hh:mm:ss) ", "");
		epochPeriod=sdf2ToDate.parse(sEpoch);
		//TODO:
		epochPeriod.setHours(1);
		
		String sDownlaodTime = fReader.readLine().replace("Download Time ", "");
		String sDownloadDate = fReader.readLine().replace("Download Date ", "");
		downloadTime=sdfToDate.parse(sDownloadDate+" "+sDownlaodTime);
		
		memoryAddress=fReader.readLine().replace("Current Memory Address: ", "");
		batteryVoltage=fReader.readLine().replace("Current Battery Voltage: ", "").replaceAll("Mode(.*)", "").trim();
		
		line = fReader.readLine();
		
		while((line = fReader.readLine()) != null)
		{
			StringTokenizer st = new StringTokenizer(line,",");
			Integer[] elements=new Integer[5];
			
			for(int i=0;i<5;i++){
				String element=st.nextToken();
				elements[i]=new Integer(element);
			}
			
			data.add(elements);
		}
		 
		fReader.close();
	}
	
	/*public static void main(String[] args){
		GT3XData gtdata=new GT3XData();
		try {
			gtdata.loadCVS("resources/file1.cvs");
		} catch (Exception e) {
			e.printStackTrace();
		}
		gtdata.getMovement();
	}*/

	public void setSerialNumber(String serialNumber) {
		this.serialNumber = serialNumber;
	}

	public String getSerialNumber() {
		return serialNumber;
	}

	public void setMemoryAddress(String memoryAddress) {
		this.memoryAddress = memoryAddress;
	}

	public String getMemoryAddress() {
		return memoryAddress;
	}

	public void setBatteryVoltage(String batteryVoltage) {
		this.batteryVoltage = batteryVoltage;
	}

	public String getBatteryVoltage() {
		return batteryVoltage;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setDownloadTime(Date downloadTime) {
		this.downloadTime = downloadTime;
	}

	public Date getDownloadTime() {
		return downloadTime;
	}

	public void setEpochPeriod(Date epochPeriod) {
		this.epochPeriod = epochPeriod;
	}

	public Date getEpochPeriod() {
		return epochPeriod;
	}
}
